﻿namespace Xml
{
    #region usings

    using System.IO;
    using System.Text;
    using System.Xml.Linq;

    #endregion

    public static class DataManager
    {
        #region Public Methods

        public static void ExportData<TData>(TData data, string fileName) where TData : new()
        {
            XDocument document = ExportData(data);
            using (var writer = new StreamWriter(fileName, encoding: Encoding.UTF8, append: false))
            {
                document.Save(writer);
                writer.Close();
            }
        }

        public static XDocument ExportData<TData>(TData data) where TData : new()
        {
            var serializer = new DataSerializer<TData>();
            XDocument document = serializer.Serialize(data, false);
            return document;
        }

        public static TData ImportData<TData>(string xml) where TData : new()
        {
            var serializer = new DataSerializer<TData>();
            return serializer.Deserialize(xml);
        }

        public static TData ImportData<TData>(XDocument document) where TData : new()
        {
            var serializer = new DataSerializer<TData>();
            return serializer.Deserialize(document);
        }

        public static TData ImportDataFromFile<TData>(string fileName) where TData : new()
        {
            var serializer = new DataSerializer<TData>();
            using (TextReader reader = new StreamReader(fileName))
            {
                return serializer.Deserialize(reader);
            }
        }

        #endregion
    }
}